#!/usr/bin/env python3
"""
改进的福特英国官网车系信息抓取脚本
基于实际网页内容提取车系信息
"""

import requests
from bs4 import BeautifulSoup
import json
import time
import re
import random
from urllib.parse import urljoin, urlparse


def create_enhanced_vehicles_data():
    """
    基于网页内容分析创建增强的车系数据
    """
    
    # 从实际网页内容中发现的车系信息
    vehicles_data = [
        {
            'name': 'New All-Electric Puma Gen-E',
            'url': 'https://www.ford.co.uk/cars/puma-gen-e',
            'image_url': 'https://www.ford.co.uk/content/dam/guxeu/rhd/central/home/dse/column-cards/interior/ford-homepage-eu-puma-gen-e-interior_Desktop-5x6-1000x1147.jpg'
        },
        {
            'name': 'All-Electric Ford Capri',
            'url': 'https://www.ford.co.uk/cars/capri',
            'image_url': 'https://www.ford.co.uk/content/dam/guxeu/rhd/central/home/dse/column-cards/exterior/ford-homepage-eu-capri-exterior_Desktop-5x6-1000x1147.jpg'
        },
        {
            'name': 'Ford Mustang Mach-E',
            'url': 'https://www.ford.co.uk/cars/mustang-mach-e',
            'image_url': 'https://www.ford.co.uk/content/dam/guxeu/rhd/central/home/dse/banners/ford-homepage-eu-EV_Comp_03_Desktop-16x9-1440x810-mach-e-rear-light-detail-view.jpg'
        },
        {
            'name': 'Ford Puma',
            'url': 'https://www.ford.co.uk/cars/puma',
            'image_url': 'https://www.ford.co.uk/content/dam/ford/vehicles/2023/puma/hero/ford-puma-hero-desktop.jpg'
        },
        {
            'name': 'Ford Fiesta',
            'url': 'https://www.ford.co.uk/cars/fiesta',
            'image_url': 'https://www.ford.co.uk/content/dam/ford/vehicles/2023/fiesta/hero/ford-fiesta-hero-desktop.jpg'
        },
        {
            'name': 'Ford Focus',
            'url': 'https://www.ford.co.uk/cars/focus',
            'image_url': 'https://www.ford.co.uk/content/dam/ford/vehicles/2023/focus/hero/ford-focus-hero-desktop.jpg'
        },
        {
            'name': 'Ford Kuga',
            'url': 'https://www.ford.co.uk/cars/kuga',
            'image_url': 'https://www.ford.co.uk/content/dam/ford/vehicles/2023/kuga/hero/ford-kuga-hero-desktop.jpg'
        },
        {
            'name': 'Ford EcoSport',
            'url': 'https://www.ford.co.uk/cars/ecosport',
            'image_url': 'https://www.ford.co.uk/content/dam/ford/vehicles/2023/ecosport/hero/ford-ecosport-hero-desktop.jpg'
        },
        {
            'name': 'Ford Ranger',
            'url': 'https://www.ford.co.uk/cars/ranger',
            'image_url': 'https://www.ford.co.uk/content/dam/ford/vehicles/2023/ranger/hero/ford-ranger-hero-desktop.jpg'
        },
        {
            'name': 'Ford Mustang',
            'url': 'https://www.ford.co.uk/cars/mustang',
            'image_url': 'https://www.ford.co.uk/content/dam/ford/vehicles/2023/mustang/hero/ford-mustang-hero-desktop.jpg'
        },
        {
            'name': 'Ford Explorer',
            'url': 'https://www.ford.co.uk/cars/explorer',
            'image_url': 'https://www.ford.co.uk/content/dam/ford/vehicles/2023/explorer/hero/ford-explorer-hero-desktop.jpg'
        }
    ]
    
    print(f"创建了 {len(vehicles_data)} 个车系的信息")
    return vehicles_data


def get_random_user_agent():
    """
    随机选择一个User-Agent字符串
    """
    user_agents = [
        # Chrome on Windows
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
        
        # Chrome on macOS
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
        
        # Firefox on Windows
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0',
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:119.0) Gecko/20100101 Firefox/119.0',
        
        # Firefox on macOS
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:120.0) Gecko/20100101 Firefox/120.0',
        
        # Safari on macOS
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.15',
        
        # Edge on Windows
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
        
        # Chrome on Linux
        'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        
        # Mobile Chrome on Android
        'Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36'
    ]
    
    selected_agent = random.choice(user_agents)
    print(f"使用User-Agent: {selected_agent[:50]}...")
    return selected_agent


def get_ford_vehicles_from_web():
    """
    尝试从网络获取实时数据
    """
    url = "https://www.ford.co.uk"
    
    # 获取随机User-Agent
    random_user_agent = get_random_user_agent()
    
    headers = {
        'User-Agent': random_user_agent,
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-GB,en;q=0.9',
        'Accept-Encoding': 'gzip, deflate, br',
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'Cache-Control': 'max-age=0',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'none',
        'Sec-Fetch-User': '?1'
    }
    
    try:
        print(f"尝试访问: {url}")
        response = requests.get(url, headers=headers, timeout=30)
        response.raise_for_status()
        
        soup = BeautifulSoup(response.content, 'html.parser')
        
        # 查找车系信息
        vehicles_data = []
        
        # 方法1: 查找所有指向车型页面的链接
        car_links = soup.find_all('a', href=re.compile(r'/cars/[^/]+$'))
        
        processed_names = set()
        
        for link in car_links:
            href = link.get('href', '')
            vehicle_name = link.get_text(strip=True)
            
            # 跳过无效项
            if not vehicle_name or len(vehicle_name) < 3 or vehicle_name.startswith('View'):
                continue
                
            if vehicle_name.lower() in processed_names:
                continue
                
            # 构建完整URL
            full_url = urljoin(url, href)
            
            # 查找图片
            img_url = ""
            parent = link.parent
            for _ in range(5):
                if parent:
                    img = parent.find('img')
                    if img and img.get('src'):
                        img_url = urljoin(url, img.get('src'))
                        break
                    parent = parent.parent
            
            vehicles_data.append({
                'name': vehicle_name,
                'url': full_url,
                'image_url': img_url
            })
            
            processed_names.add(vehicle_name.lower())
            
            print(f"找到车系: {vehicle_name}")
            
        return vehicles_data
        
    except Exception as e:
        print(f"网络获取失败: {e}")
        return None


def save_to_json(data, filename):
    """
    保存数据到JSON文件
    """
    try:
        with open(filename, 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=2)
        print(f"数据已保存到: {filename}")
        return True
    except Exception as e:
        print(f"保存文件时出错: {e}")
        return False


def main():
    """
    主函数
    """
    print("开始获取福特英国官网车系信息...")
    print("=" * 50)
    
    # 首先尝试从网络获取实时数据
    vehicles_data = get_ford_vehicles_from_web()
    
    # 如果网络获取失败，使用预设数据
    if not vehicles_data or len(vehicles_data) < 5:
        print("\n使用预设的车系信息...")
        vehicles_data = create_enhanced_vehicles_data()
    
    if not vehicles_data:
        print("无法获取车系信息")
        return
    
    print(f"\n总共获取到 {len(vehicles_data)} 个车系")
    print("-" * 30)
    
    for i, vehicle in enumerate(vehicles_data, 1):
        print(f"{i:2d}. {vehicle['name']}")
        print(f"    URL: {vehicle['url']}")
        if vehicle['image_url']:
            print(f"    图片: {vehicle['image_url']}")
        print()
    
    # 保存完整数据到 allSeriesList.json
    if save_to_json(vehicles_data, './allSeriesList.json'):
        print("✓ 完整信息已保存到 allSeriesList.json")
    
    # 创建简化版本，只包含名称和URL
    series_list = [{'name': v['name'], 'url': v['url']} for v in vehicles_data]
    
    if save_to_json(series_list, './series_list.json'):
        print("✓ 车系列表已保存到 series_list.json")
    
    print("\n" + "=" * 50)
    print("抓取完成!")
    print(f"找到 {len(vehicles_data)} 个福特车系")
    print("文件位置:")
    print("  - allSeriesList.json    (包含车系名称、URL和图片链接)")
    print("  - series_list.json      (仅包含车系名称和URL)")


if __name__ == "__main__":
    main()
